<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class VulsCode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'vulscode:year {year?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'creat vuls number';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $year = $this->argument('year') ? $this->argument('year') : date('Y');
        $years=$this->selectYear($year);
        if ($this->confirm('Do you wish to continue? [y|N]')) {
            $sql = "select * from vuls where date_format(created_at,'%Y')=:mytime";
            $vuls = DB::select($sql,['mytime' => $year]);

            foreach ($vuls as $a) {
                $code = "";
                $code = trim($a->vuls_number, " ");
                $hve=strstr($code,"HVE");
                if (!$code || !$hve) {
                    DB::beginTransaction();
                    try {
                        $newcode = $this->creatVulsCode($year);
                        $result1 = DB::table('vuls')
                            ->where('id', $a->id)
                            ->update(['vuls_number' => $newcode]);
                        if (!$result1) {
                            /**
                             * Exception类接收的参数
                             * $message = "", $code = 0, Exception $previous = null
                             */
                            throw new \Exception("1");
                        }
                        $result2 = DB::insert("INSERT INTO vuls_count (year,count) VALUES ($year,1) ON DUPLICATE KEY UPDATE count=count+1");
                        if (!$result2) {
                            throw new \Exception("2");
                        }
                        DB::commit();
                        $this->info("id:" . $a->id . "   vuls_number:" . $newcode . "\n");
                    } catch (\Exception $e) {
                        DB::rollback();//事务回滚
                    }

                }

            }

        }
    }

    /**生成漏洞编码
     * @return string
     */
    private function creatVulsCode($years)
    {
        $vuls = DB::table('vuls_count')
            ->where('year', $years)
            ->select('count')
            ->get()
            ->toArray();
        $codeY=$this->selectYear($years);
        if (empty($vuls)) {
            $code = "HVE-" . $codeY . '-0001';
        } else {
            $count = $vuls[0]->count;
            $count += 1;
            $num = str_pad($count, 4, 0, STR_PAD_LEFT);
            $code = "HVE-" . $codeY . '-' . $num;
        }
        return $code;

    }
    private function  selectYear($year)
    {
        $result=strpos($year,"0",0);
        if($result)
        {
            $result=$result+1;
        }
        $years=substr($year,$result);
        return $years;
    }
}
